package com.example.module.web;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.example.R;

public class CustomAddressBar extends FrameLayout {
	private static final String LOG_TAG = "CustomAddressBar";
//	private ImageView navigationButton;
	private View addressProgressBar;
	private EditText addressText;
	private ImageView bookmarkButton;
	private Drawable navigationDrawable, removeDrawable;
	private WebViewListener webViewListener;
	private ImageView menuButton;
	private ImageView postButton;
	
	public interface WebViewListener {
		public void onLoad(String url);
		public void onLoadFinished();
	}
	
	public CustomAddressBar(Context context) {
		super(context);
		initialize(context);
	}
	
	public CustomAddressBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		initialize(context);
	}

	private void initialize(Context context) {
		FrameLayout addressBar = (FrameLayout) inflate(context, R.layout.address_bar, this);
		
		addressText = (EditText) addressBar.findViewById(R.id.addressText);
		bookmarkButton = (ImageView) addressBar.findViewById(R.id.bookmarkButton);
		
		menuButton = (ImageView) addressBar.findViewById(R.id.menuButton);
		postButton = (ImageView) addressBar.findViewById(R.id.postButton);
		
		
//		navigationButton = (ImageView) addressBar.findViewById(R.id.navigationButton);
//		navigationButton.setOnClickListener(new View.OnClickListener() {
//			
//			@Override
//			public void onClick(View view) {
//				quickAction.show(view);
//			}
//		});
		
		// setup Address Progress Bar
		addressProgressBar = addressBar.findViewById(R.id.addressProgreeBar);
		addressProgressBar.getBackground().setAlpha(127);
		
		addressText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View view, boolean flag) {
				Log.d(LOG_TAG, "onFocusChange flag=" + flag);
				
				if (flag) {
//					navigationButton.setImageDrawable(removeDrawable);
//					navigationButton.setOnClickListener(new View.OnClickListener() {
//						
//						@Override
//						public void onClick(View view) {
//							addressText.setText("");
//						}
//					});
				}
				else {
//					navigationButton.setImageDrawable(navigationDrawable);
//					navigationButton.setOnClickListener(new View.OnClickListener() {
//						
//						@Override
//						public void onClick(View view) {
//							quickAction.show(view);
//						}
//					});

					hideInputKeypad();
				}
			}
		});
		
		addressText.setOnEditorActionListener(new OnEditorActionListener() {
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				Log.d(LOG_TAG, "onEditorAction actionId=" + actionId + " event=" + event);
				
				if (actionId == EditorInfo.IME_ACTION_GO) {
		        	String url = addressText.getText().toString();
					if (!url.startsWith("http://")) url = "http://" + url;
					if (webViewListener != null)
						webViewListener.onLoad(url);
					
					hideInputKeypad();
		            return true;
		        }
		        return false;
			}
		});
		
		navigationDrawable = getResources().getDrawable(R.drawable.navi);
		removeDrawable = getResources().getDrawable(R.drawable.delete);        
	}

	public View getAddressProgressBar() {
		return addressProgressBar;
	}
	public void setAddressProgressBar(View addressProgressBar) {
		this.addressProgressBar = addressProgressBar;
	}

	public EditText getAddressText() {
		return addressText;
	}
	public void setAddressText(EditText addressText) {
		this.addressText = addressText;
	}

	public ImageView getBookmarkButton() {
		return bookmarkButton;
	}
	public void setBookmarkButton(ImageView bookmarkButton) {
		this.bookmarkButton = bookmarkButton;
	}

	public void changeAddressProgress(final int progress) {
		int newProgress = (int) Math.sqrt(10000 - Math.pow(progress - 100, 2)); 
		int width = addressText.getWidth() * newProgress  / 100;
		if (newProgress >= 100) {
			width = 0;
			if (webViewListener != null)
				webViewListener.onLoadFinished();
		}
			
		else if (width < 10)
			width = FrameLayout.LayoutParams.WRAP_CONTENT;
		
		FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) addressProgressBar.getLayoutParams();
		params.width = width;
		addressProgressBar.setLayoutParams(params);
		
//		increaseProgressBar(width);
	}
	
	public void increaseProgressBar(final int width) {
		addressProgressBar.post(new Runnable() {
			
			@Override
			public void run() {
				if (width <= 0) {
					FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) addressProgressBar.getLayoutParams();
					params.width = width;
					addressProgressBar.setLayoutParams(params);
				}
				else {
					int mWidth = 0;
					while (mWidth < width) {
						FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) addressProgressBar.getLayoutParams();
						mWidth = params.width + 2; 
						params.width = mWidth;
						addressProgressBar.setLayoutParams(params);
					}	
				}
			}
		});
	}
	
	public void hideInputKeypad() {
		InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(addressText.getWindowToken(), 0);
	}

	public WebViewListener getWebViewListener() {
		return webViewListener;
	}

	public void setWebViewListener(WebViewListener webViewListener) {
		this.webViewListener = webViewListener;
	}

	public void setAddressUrl(String url) {
		this.addressText.setText(url);
	}

	public ImageView getMenuButton() {
		return menuButton;
	}

	public void setMenuButton(ImageView menuButton) {
		this.menuButton = menuButton;
	}

	public ImageView getPostButton() {
		return postButton;
	}

	public void setPostButton(ImageView postButton) {
		this.postButton = postButton;
	}
}
